<?php

/*
 * Niken Larasati
 * larasati.n@students.amikom.ac.id
 */

class kelahiran extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('tanggal');
    }

    function form_kelahiran() {
        $this->load->model('umum_m');
        $data['jenis_kelamin'] = $this->umum_m->ambil_data('jenis_kelamin');
        $data['jenis_kelahiran'] = $this->umum_m->ambil_data('jenis_kelahiran');

        $this->template->load('halaman_v', 'kelahiran/tambah_kelahiran_v', $data);
    }

    function tambah() {
        if ($this->input->post('tambah') != NULL) {
            $data['idkelahiran'] = $this->input->post('idkelahiran');
            $data['no_form'] = $this->input->post('no_form');
            $data['no_kk'] = $this->input->post('no_kk');
            $data['nama'] = $this->input->post('namabayi');
            $data['idjenis_kelamin'] = $this->input->post('idjenis_kelamin');
            $data['tempat_dilahirkan'] = $this->input->post('tempat_dilahirkan');
            $data['tempat_kelahiran'] = $this->input->post('tempat_kelahiran');
            $data['tanggal'] = $this->input->post('tgl_lahir');
            $data['pukul'] = $this->input->post('pukul');
            $data['idjenis_kelahiran'] = $this->input->post('idjenis_kelahiran');
            $data['kelahiran_ke'] = $this->input->post('kelahiran_ke');
            $data['penolong_kelahiran'] = $this->input->post('penolong');
            $data['berat_bayi'] = $this->input->post('brt_bayi');
            $data['panjang_bayi'] = $this->input->post('pjg_bayi');
            $data['nik_ibu'] = $this->input->post('nik_ibu');
            $data['nik_ayah'] = $this->input->post('nik_ayah');
            $data['nik_pelapor'] = $this->input->post('nik_pelapor');
            $data['hub_dgbayi'] = $this->input->post('hub_pelapor');
            $data['nik_saksi_satu'] = $this->input->post('nik_saksi_satu');
            $data['nik_saksi_dua'] = $this->input->post('nik_saksi_dua');


            $this->load->model('umum_m');
            $this->umum_m->tambah('kelahiran', $data);
            $this->tampil();
        } else {
            echo 'gagal';
        }
    }

    function ubah($id = NULL) {
        $this->load->model('umum_m');
        $data['jenis_kelamin'] = $this->umum_m->ambil_data('jenis_kelamin');
        if ($id != NULL) {
            $this->load->model('umum_m');
            $data['data_kelahiran'] = $this->umum_m->ambil_data('kelahiran', 'idkelahiran = ' . $id)->row();
            $this->template->load('halaman_v', 'kelahiran/ubah_kelahiran_v', $data);
        } else {
            redirect('kelahiran');
        }
    }

    function simpan_ubah() { //kalau data tidak diubah,maka....?
        if ($this->input->post() != null) {
            $kelahiran['idkelahiran'] = $this->input->post('idkelahiran');
            $kelahiran['no_form'] = $this->input->post('no_form');
            $kelahiran['no_kk'] = $this->input->post('no_kk');
            $kelahiran['nama'] = $this->input->post('namabayi');
            $kelahiran['idjenis_kelamin'] = $this->input->post('idjenis_kelamin');
            $kelahiran['tempat_dilahirkan'] = $this->input->post('tempat_dilahirkan');
            $kelahiran['tempat_kelahiran'] = $this->input->post('tempat_kelahiran');
            $kelahiran['tanggal'] = $this->input->post('tgl_lahir');
            $kelahiran['pukul'] = $this->input->post('pukul');
            $kelahiran['idjenis_kelahiran'] = $this->input->post('idjenis_kelahiran');
            $kelahiran['kelahiran_ke'] = $this->input->post('kelahiran_ke');
            $kelahiran['penolong_kelahiran'] = $this->input->post('penolong');
            $kelahiran['berat_bayi'] = $this->input->post('brt_bayi');
            $kelahiran['panjang_bayi'] = $this->input->post('pjg_bayi');
            $kelahiran['nik_ibu'] = $this->input->post('nik_ibu');
            $kelahiran['nik_ayah'] = $this->input->post('nik_ayah');
            $kelahiran['nik_pelapor'] = $this->input->post('nik_pelapor');
            $kelahiran['hub_dgbayi'] = $this->input->post('hub_pelapor');
            $kelahiran['nik_saksi_satu'] = $this->input->post('nik_saksi_satu');
            $kelahiran['nik_saksi_dua'] = $this->input->post('nik_saksi_dua');

            $this->load->model('umum_m');
            if ($this->umum_m->ubah('kelahiran', $kelahiran, 'idkelahiran', $kelahiran['idkelahiran'])) {
                redirect('kelahiran/tampil');
            }
        }
    }

    function tampil() {
        $this->load->model('kelahiran_m');
        $data['kelahiran'] = $this->kelahiran_m->ambil_data();

        $data['nama_kk'] = $this->kelahiran_m->nama_kk();
        $this->template->load('halaman_v', 'kelahiran/tampil_kelahiran_v', $data);
    }

    function hapus($id = NULL) {
        $this->load->model('umum_m');
        $this->umum_m->hapus_data_tabel('kelahiran', array('idkelahiran' => $id));
        $this->tampil();
    }

    function cari_jk() {
        $this->load->model('umum_m');
        $data['jenis_kelamin'] = $this->umum_m->ambil_data('jenis_kelamin');
        if ($this->input->post() != NULL) {
            $this->load->model('kelahiran_m');
            $data['kelahiran'] = $this->kelahiran_m->detail_cari('kelahiran.idjenis_kelamin', $this->input->post('idjenis_kelamin'));
            $data['nama_jeniskelamin'] = $this->umum_m->native_query('select jenis_kelamin from jenis_kelamin where idjenis_kelamin=' . $this->input->post('idjenis_kelamin'))->row();
            $this->template->load('halaman_v', 'kelahiran/cari_jkkelahiran_v', $data);
        } else {
            $this->load->model('kelahiran_m');
            $data['kelahiran'] = $this->kelahiran_m->detail_cari();
            $this->template->load('halaman_v', 'kelahiran/cari_jkkelahiran_v', $data);
        }
    }

    function cari_form() {
        $this->load->model('umum_m');
        $data['kelahiran'] = $this->umum_m->ambil_data('kelahiran');
        $data['penduduk'] = $this->umum_m->ambil_data('penduduk');
        if ($this->input->post() != NULL) {
            $this->load->model('kelahiran_m');
            $this->load->model('penduduk_m');
            $data['kelahiran'] = $this->kelahiran_m->detail_cari('kelahiran.no_form', $this->input->post('no_form'));
//            $data['penduduk'] = $this->kelahiran_m->detail_cari(array('penduduk.nik' => '"' . $this->input->post('no_form') . '"'));
            $data['nomor_form'] = $this->umum_m->native_query('select no_form from kelahiran where no_form=' . '"' . $this->input->post('no_form') . '"')->row();
            $this->template->load('halaman_v', 'kelahiran/cari_formkelahiran_v', $data);
        } else {
            $this->load->model('kelahiran_m');
            $this->load->model('penduduk_m');
            $data['kelahiran'] = $this->kelahiran_m->detail_cari();
            $data['penduduk'] = $this->penduduk_m->detail_cari();
            $this->template->load('halaman_v', 'kelahiran/cari_formkelahiran_v', $data);
        }
    }

    function cetak_suratketerangan() {
        $this->load->model('umum_m');
        $data['kelahiran'] = $this->umum_m->ambil_data('kelahiran');
        //$data['penduduk'] = $this->umum_m->ambil_data('penduduk');
        if ($this->input->post() != NULL) {
            $this->load->model('kelahiran_m');
            $this->load->model('penduduk_m');
            $data['kelahiran'] = $this->kelahiran_m->detail_cari(array('kelahiran.no_form' => '"' . $this->input->post('no_form') . '"'));
            $data['penduduk'] = $this->penduduk_m->detail_cari(array('penduduk.nik' => '"' . $this->input->post('no_form') . '"'));
            $data['nomor_form'] = $this->umum_m->native_query('select no_form from kelahiran where no_form=' . '"' . $this->input->post('no_form') . '"')->row();
            $this->template->load('halaman_v', 'kelahiran/cetak_formkelahiran_v', $data);
        } else {
            $this->load->model('kelahiran_m');
            $data['kelahiran'] = $this->kelahiran_m->detail_cari();
            $this->template->load('halaman_v', 'kelahiran/cetak_formkelahiran_v', $data);
        }
    }

}

?>
